package com.example.c7.servlet;

import com.example.c7.bean.NewsBean;
import com.example.c7.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/news.jsp")
public class NoticeUpdateServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String newsTitle = req.getParameter("upd_NewsTitle");
        String newsContent = req.getParameter("upd_NewsContent");
        int newsId = Integer.parseInt(req.getParameter("newsId"));
        NewsBean NewsBean = new NewsBean();
        NewsBean.setNewsID(newsId);
        NewsBean.setNewsTitle(newsTitle);
        NewsBean.setNewsContent(newsContent);
        NewsBean.setAdminName("admin"); // todo 登录管理员名字
        NewsDao newsDao = new NewsDao();
        int result = newsDao.update(NewsBean);
        if (result>0){
            resp.sendRedirect("news.jsp");
        }else {
            req.setAttribute("msg","更新失败");
            req.setAttribute("url","news.jsp");
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}
